package cjm.task5.config;

import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component("myAuthenticationSuccessHandler")
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest,
                                        HttpServletResponse httpServletResponse,
                                        Authentication authentication
    ) throws IOException, ServletException {
       // System.out.println("登录成功");
        String who = String.valueOf(authentication.getAuthorities());
        //System.out.println(authentication.getAuthorities());
        //System.out.println(who);
        String userid = httpServletRequest.getParameter("userid");
        String userPassword = httpServletRequest.getParameter("userPassword");
        //System.out.println("userid：" + userid);
        //System.out.println("userPassword:" + userPassword);
        String uri = "/doLogin?userid=" + userid + "&userPassword=" + userPassword;
        httpServletResponse.sendRedirect(uri);
        /*
        if(!(who.equals("[ROLE_ADMIN]")||who.equals("[ROLE_USER]"))){
            httpServletResponse.sendRedirect("/login");
        }else {
            String uri = "/doLogin?userid=" + userid + "&userPassword=" + userPassword;
            httpServletResponse.sendRedirect(uri);
        }*/

        /*if (who.equals("[ROLE_ADMIN]")){
            httpServletResponse.sendRedirect("/searchAllRecord");
        }
        else if(who.equals("[ROLE_USER]")){
            httpServletResponse.sendRedirect("/yqfk/" + userid);
        }
        else {
            httpServletResponse.sendRedirect("/login");
        }*/

    }
}
